// __PUBLIC__/js/page/admin/goods/goodslist
require(['../../common'], function() {
  require(['goods.goodslist'])
})

define('goods.goodslist', [
  'jquery',
  'page/grid'
], function($, Grid) {
  'use strict';
  var $table = $('.table')
  var mygrid = new Grid($table)
  mygrid.init()
  function getSelected() {
    var selected = mygrid.getSelected()
    if (!selected.count) {
      alert(lang.pleaseSelectAData)
      return
    }
    return selected
  }

  function getASelected(t) {
    var current = mygrid.getCurrent(t)
    var selected = {
      id: [current.id],
      index: [current.index]
    }
    console.log(selected)
    return selected
  }

  $(document)
    .on('click', '.A_del', function(e) {
      e.preventDefault()
      deletegoods(getASelected(this))
    })
    .on('click', '#J_del', function(e) {
      e.preventDefault()
      var selected = getSelected()
      deletegoods(selected)
    })
    .on('click', '.A_dodel', function(e) {
      e.preventDefault()
      delgoods(getASelected(this))
    })
    .on('click', '#J_dodel', function(e) {
      e.preventDefault()
      var selected = getSelected()
      delgoods(selected)
    })
    .on('click', '#J_search', function(e) {
      e.preventDefault()
      searchData()
    })
     .on('click', '#A_downgoods', function(e) {
       e.preventDefault()
       if(downGoods(getASelected(this))){  
          if($('#status').val() ==4){
    	     var id =getASelected(this).id.join();
    	     $(this).attr("id","A_upgoods") 
    	     $(this).text('上线')
    	     $('#is_del'+id).text('已下架')
          }else{
    	      mygrid.del(getASelected(this).index)
          }
       }
    })
     .on('click', '#J_batchdown', function(e) {
        e.preventDefault()
        var selected = getSelected()
        downGoods(selected)
    })
    .on('click', '#A_upgoods', function(e) {
      e.preventDefault()
      if(upGoods(getASelected(this))){
        if($('#status').val() ==4){
    	    var id =getASelected(this).id.join() 
    	    $(this).attr("id","A_downgoods") 
    	    $(this).text('下架')
    	    $('#is_del'+id).text('已上线')
         }else{
    	     mygrid.del(getASelected(this).index)
         }
       } 
    })
     .on('click', '#J_batchup', function(e) {
      e.preventDefault()
       var selected = getSelected()
      upGoods(selected)
    })

  function deletegoods(selected) {
    $.post(APP + '?app=admin&m=Goods&a=doDeleteGoods', {
      ids: selected.id.join()
    })
      .done(function(res) {
        if (res == '0') {
          alert(lang.delFails)
        } else {
          /*$('#sysdeleteuser').html(res)*/
          alert(lang.delSuccess)
          mygrid.del(selected.index)
        }
      })
  }
  
  // 永久删除动作
  function delgoods(selected) {
	    $.post(APP + '?app=admin&m=Goods&a=doDelGoods', {
	      ids: selected.id.join()
	    })
	      .done(function(res) {
	        if (res == '0') {
	          alert(lang.delFails)
	        } else {
	          /*$('#sysdeleteuser').html(res)*/
	          alert(lang.delSuccess)
	          mygrid.del(selected.index)
	        }
	      })
	  }
  
  function downGoods(selected) {
	    var flag =true;
	    $.post(APP + '?app=admin&m=Goods&a=doDownGoods', {
	      ids: selected.id.join()
	    })
	      .done(function(res) {
	        if (res == '0') {
	          alert("下架失败！")
	          flag = false;
	        } else {
	          /*$('#sysdeleteuser').html(res)*/
	          alert("成功下架！")
	          //mygrid.del(selected.index)
	          flag = true
	        }
	      })
	    return flag;
	  }
  
  function upGoods(selected) {
	    var flag = true;
	    $.post(APP + '?app=admin&m=Goods&a=doUpGoods', {
	      ids: selected.id.join()
	    })
	      .done(function(res) {
	        if (res == '0') {
	          flag =false;
	          alert("上线失败！")
	        } else {
	          /*$('#sysdeleteuser').html(res)*/
	          alert("上线成功！")
	        //  mygrid.del(selected.index)
	          flag = true;
	        }
	      })
	      return flag;
	  }

  function searchData() {
    var name = $('#name').val()
    var cate_id = $('#cate_id').val()
    var status = $('#status').val()
    var par_get = '&name=' + name
    par_get += '&cate_id=' + cate_id
    par_get += '&status=' + status
    location.href = APP + '?app=admin&m=Goods&a=goodsList' + par_get
  }

})
